package com.wetao.education.module_reader.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.education.module_reader.R;
import com.wetao.education.module_reader.contract.ReadPushCon;
import com.wetao.lib_common.bean.response.ReadPushListInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * author: zhouyh
 * created on: 2020/11/11 4:20 PM
 * description:
 */
public class ReadPushPre extends ReadPushCon.Presenter {

    private Context mContext;

    public ReadPushPre(Context mContext) {
        this.mContext = mContext;
    }



    @Override
    public void getReadPushList(HashMap<String, String> map, boolean isDialog, boolean isCancelable, final int cur) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getStudentReadPushList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<ReadPushListInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<ReadPushListInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<ReadPushListInfo>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(ReadPushListInfo baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getReadPushListResult(baseResponse,cur);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().getReadPushListFailed(cur);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        getView().getReadPushListFailed(cur);
                    }
                });
    }
}
